package Gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

import javax.swing.ImageIcon;
import javax.swing.JComponent;

import Snake.Game;


@SuppressWarnings("serial")
public class Board extends JComponent {


	private Game game;
	private int nLines;
	private int nCol;
	private int selected = -1;
	private int boardWidth;
	private int linPoint = 0;
	private int ColPoint = 0;

	private Image barrierIcon;
	private Image jumpIcon;
	private Image foodIcon;
	private Image headIcon;
	private Image bodyIcon;
	

	public int getBoardWidth() {
		return boardWidth;
	}

	public Board(int nDimens) {
		super();
		this.nLines = nDimens;
		this.nCol = nDimens;
		game = new Game(this);
		loadImages();

	}

	public int getnCol() {
		return nCol;
	}
	

	@Override
	public void paintComponent(Graphics g) {
		
		synchronized (game) {
			super.paintComponent(g);
			this.boardWidth = getWidth();


			doDrawingLine(g);
			doDrawingBarrier(g);
			doDrawingJump(g);
			doDrawingFood(g);
			doDrawingSnake(g);
			selectSnake(g);	
			repaint();
		}
		
	}

	// Escolhe um ponto aleatorio para poder criar os objetos...
	public void setSelected(Point point) {

		System.out.println(point.getX() + " Y: " + point.getY());
		ColPoint = (int) (point.getX() / (getWidth() / nCol));
		linPoint = (int) (point.getY() / (getHeight() / nLines));
		selected = ColPoint + linPoint * nCol;
		repaint();
	}

	// Carregamento das imagens..
	private void loadImages() {

		ImageIcon imgJump = new ImageIcon(
				"src/img/jump20x20.png");
		jumpIcon = imgJump.getImage();

		ImageIcon imagFood = new ImageIcon(
				"src/img/food20x20.png");
		foodIcon = imagFood.getImage();

		ImageIcon iBarrier = new ImageIcon(
				"src/img/Barrier20x20.png");
		barrierIcon = iBarrier.getImage();
		
		ImageIcon headSnake = new ImageIcon(
				"src/img/head.png");
		headIcon = headSnake.getImage();
		
		ImageIcon bodySnake = new ImageIcon(
				"src/img/body.png");
		bodyIcon = bodySnake.getImage();
	}
	
	//***** Metodos de pintar os objetos ****//
	private void doDrawingFood(Graphics g) {
		for (int i = 0; i < game.getListfood().size(); i++) {
			g.drawImage(
					foodIcon,
					(game.getMatrizGame()[game.getListfood().get(i).getX()][game
							.getListfood().get(i).getY()].getX() * (getWidth() / 30)),
					(game.getMatrizGame()[game.getListfood().get(i).getX()][game
							.getListfood().get(i).getY()].getY() * (getWidth() / 30)),
					this);
		}
	}
	
	private void doDrawingJump(Graphics g) {
		for (int i = 0; i < game.getListJump().size(); i++) {
			g.drawImage(
					jumpIcon,
					(game.getMatrizGame()[game.getListJump().get(i).getX()][game
							.getListJump().get(i).getY()].getX() * (getWidth() / 30)),
					(game.getMatrizGame()[game.getListJump().get(i).getX()][game
							.getListJump().get(i).getY()].getY() * (getWidth() / 30)),
					this);
		}
	}
	
	private void doDrawingBarrier(Graphics g) {
		for (int i = 0; i < game.getListBarrier().size(); i++) {
			g.drawImage(
					barrierIcon,
					(game.getMatrizGame()[game.getListBarrier().get(i).getX()][game
							.getListBarrier().get(i).getY()].getX() * (getWidth() / 30)),
					(game.getMatrizGame()[game.getListBarrier().get(i).getX()][game
							.getListBarrier().get(i).getY()].getY() * (getWidth() / 30)),
					this);
		}
	}
	
	private void doDrawingLine(Graphics g){
		int dx = getWidth() / (nCol);
		int dy = getHeight() / (nLines);
		for (int i = 1; i < nLines; i++) {
			g.drawLine(0, i * dy, getWidth(), i * dy);
		}
		for (int i = 1; i < nCol; i++) {
			g.drawLine(i * dx, 0, i * dx, getHeight());
		}
	}

	private void doDrawingSnake(Graphics g) {
		
		for (int i = 0; i < game.getListSnake().size(); i++) {
				
			for (int j = 0; j < game.getListSnake().get(i).getSizeSnake().size(); j++) { //Controlar MARGEM
				g.drawImage(headIcon, (game.getMatrizGame()[game.getListSnake().get(i).getSizeSnake().getFirst().getX()]
						[game.getListSnake().get(i).getSizeSnake().getFirst().getY()].getX() * (getWidth() / 30)), 
						(game.getMatrizGame()[game.getListSnake().get(i).getSizeSnake().getFirst().getX()]
								[game.getListSnake().get(i).getSizeSnake().getFirst().getY()].getY() * (getWidth() / 30)), this);
//				System.out.println("Acabei de pintar a Cabeça da cobra");
		
				// cauda!
//				System.out.println(game.getListSnake().get(i).getSizeSnake().get(i).toString());
				if(j >= 1){
					g.drawImage(bodyIcon, (game.getMatrizGame()[game.getListSnake().get(i).getSizeSnake().get(j).getX()]
							[game.getListSnake().get(i).getSizeSnake().get(j).getY()].getX() * (getWidth() / 30)), 
							(game.getMatrizGame()[game.getListSnake().get(i).getSizeSnake().get(j).getX()]
									[game.getListSnake().get(i).getSizeSnake().get(j).getY()].getY() * (getWidth() / 30)), this);
//					System.out.println("vou começar a pintar a cauda!");
				}

			}



		}


	}

	private void selectSnake(Graphics g) {		
		
		int dx = getWidth() / (nCol);
		int dy = getHeight() / (nLines);

		if (selected > -1) {// seleçao da cobra para mudar de cor.			
			int x = (selected % nCol);
			int y = (selected / nCol);
			for (int i = 0; i < game.getListSnake().getFirst().getSizeSnake().size(); i++) {
				if(game.getListSnake().getFirst().isSnake(game.getMatrizGame()[x][y])){
					int xSnake = game.getListSnake().getFirst().getSizeSnake().get(i).getX();
					int ySnake = game.getListSnake().getFirst().getSizeSnake().get(i).getY();
					// g.setColor(Color.DARK_GRAY);
					g.setColor(new Color(0.0f, 0.5f, 0.1f, 0.5f));// transparencia
					g.fillRect((int) ((xSnake) * dx),
							(int) ((ySnake) * dy), dx, dy);
				}
			}

		}
	}


}